package com.tools.common.object;

import java.util.Date;

/**
 * 比较运算符的枚举类
 * */
@Note("比较运算符的枚举类")
public enum ContrastEnum {
    LT("<"),

    LTE("<="),

    GT(">"),

    GTE(">="),

    EQ("=="),

    NE("!=");

    private final String symbol;

    ContrastEnum(String symbol) {
        this.symbol = symbol;
    }

    public String getSymbol() {
        return symbol;
    }

    @Override
    public String toString() {
        return this.name();
    }

    /* *******************************************************************************************
     *
     *          比较操作
     *
     * *******************************************************************************************
     * */


    @Note("比较两个 byte 值")
    public boolean compareTo(byte left, byte right) {
        switch (this) {
            case NE: return left != right;
            case EQ: return left == right;
            case LT: return left < right;
            case LTE: return left <= right;
            case GT: return left > right;
            case GTE: return left >= right;
        }
        throw new IllegalArgumentException("不知道 【" + this + "】 是什么枚举");
    }


    @Note("比较两个 short 值")
    public boolean compareTo(short left, short right) {
        switch (this) {
            case EQ: return left == right;
            case NE: return left != right;
            case LT: return left < right;
            case LTE: return left <= right;
            case GT: return left > right;
            case GTE: return left >= right;
        }
        throw new IllegalArgumentException("不知道 【" + this + "】 是什么枚举");
    }


    @Note("比较两个 int 值")
    public boolean compareTo(int left, int right) {
        switch (this) {
            case LT: return left < right;
            case LTE: return left <= right;
            case GT: return left > right;
            case GTE: return left >= right;
            case EQ: return left == right;
            case NE: return left != right;
        }
        throw new IllegalArgumentException("不知道 【" + this + "】 是什么枚举");
    }


    @Note("比较两个 long 值")
    public boolean compareTo(long left, long right) {
        switch (this) {
            case LTE: return left <= right;
            case LT: return left < right;
            case GTE: return left >= right;
            case GT: return left > right;
            case EQ: return left == right;
            case NE: return left != right;
        }
        throw new IllegalArgumentException("不知道 【" + this + "】 是什么枚举");
    }


    @Note("比较两个 float 值")
    public boolean compareTo(float left, float right) {
        switch (this) {
            case LTE: return left <= right;
            case LT: return left < right;
            case GT: return left > right;
            case GTE: return left >= right;
            case EQ: return left == right;
            case NE: return left != right;
        }
        throw new IllegalArgumentException("不知道 【" + this + "】 是什么枚举");
    }

    @Note("比较两个 double 值")
    public boolean compareTo(double left, double right) {
        switch (this) {
            case LT: return left < right;
            case LTE: return left <= right;
            case GTE: return left >= right;
            case GT: return left > right;
            case EQ: return left == right;
            case NE: return left != right;
        }
        throw new IllegalArgumentException("不知道 【" + this + "】 是什么枚举");
    }


    @Note("比较两个 char 值")
    public boolean compareTo(char left, char right) {
        switch (this) {
            case LT: return left < right;
            case LTE: return left <= right;
            case EQ: return left == right;
            case NE: return left != right;
            case GTE: return left >= right;
            case GT: return left > right;
        }
        throw new IllegalArgumentException("不知道 【" + this + "】 是什么枚举");
    }


    @Note("比较两个 Date 日期时间，其实就是比较时间戳")
    public boolean compareTo(Date left, Date right) {
        if(left == null || right == null) return false;
        return compareTo(left.getTime(), right.getTime());
    }

    /* *******************************************************************************************
     *
     *          静态方法区
     *
     * *******************************************************************************************
     * */


    @Note("安全地根据字符串符号获取对应的枚举常量，获取不到不会抛异常")
    public ContrastEnum getEnumSafe(String symbol) {
        if(LT.symbol.equalsIgnoreCase(symbol)) {
            return LT;
        }
        if(LTE.symbol.equalsIgnoreCase(symbol)) {
            return LTE;
        }
        if(GT.symbol.equalsIgnoreCase(symbol)) {
            return GT;
        }
        if(GTE.symbol.equalsIgnoreCase(symbol)) {
            return GTE;
        }
        if(EQ.symbol.equalsIgnoreCase(symbol)) {
            return EQ;
        }
        if(NE.symbol.equalsIgnoreCase(symbol)) {
            return NE;
        }
        return null;
    }

    @Note("根据字符串符号获取对应的枚举常量，获取不到会抛异常")
    public ContrastEnum getEnum(String symbol) {
        ContrastEnum res = getEnumSafe(symbol);
        if(res == null) throw new IllegalArgumentException("不知道 【" + symbol + "】 属于什么枚举类型");
        return res;
    }
}
